package com.eastday.dao.impl;
import com.eastday.util.ParseConfigFactory;
import com.eastday.util.ExceptionHandler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.jdom.JDOMException;


/**
 * 
 * @author dingxs
 * 
 * 
 */
public class ConnectionBuilderDao  {

	protected Connection conn;
	protected ExceptionHandler exceptionHandler;
	
	protected ConnectionBuilderDao(){
		exceptionHandler = new ExceptionHandler();
	}
	
	
	protected void conntoOracleDb() throws ClassNotFoundException, SQLException, JDOMException, IOException{
		ParseConfigFactory.constructSysConfig();
		Class.forName("oracle.jdbc.driver.OracleDriver");
		String dbLink = "jdbc:oracle:thin:@";
		dbLink = dbLink+ParseConfigFactory.sysConfigModel.getHostip()+":"+ParseConfigFactory.sysConfigModel.getHostport()+":"+ParseConfigFactory.sysConfigModel.getDbname();
		this.setConn(DriverManager.getConnection(dbLink,ParseConfigFactory.sysConfigModel.getUsername(),ParseConfigFactory.sysConfigModel.getPassword()));
	}
	
	
	protected void conntoMysqlDb() throws ClassNotFoundException, SQLException, JDOMException, IOException{
		ParseConfigFactory.constructSysConfig();
		Class.forName("com.mysql.jdbc.Driver");
		String dbLink = "jdbc:mysql://";
		dbLink = dbLink+ParseConfigFactory.sysConfigModel.getHostip()+":"+ParseConfigFactory.sysConfigModel.getHostport()+"/"+ParseConfigFactory.sysConfigModel.getDbname();
		this.setConn(DriverManager.getConnection(dbLink,ParseConfigFactory.sysConfigModel.getUsername(),ParseConfigFactory.sysConfigModel.getPassword()));
	}
	

	protected Connection getConn() {
		return conn;
	}



	protected void setConn(Connection conn) {
		this.conn = conn;
	}
	
	
	protected void closeConnection(){
		try {
			this.conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.exceptionHandler.writeToLog(e.getMessage());
		}
	}

	
	
	

	

	
	
	
	
}
